package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import services.CdaService;
import services.CenterCRUDService;
import services.InvestigadorProtocoloCentroService;
import services.ProtocolCRUDService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 05/09/12
 * Time: 09:45
 * To change this template use File | Settings | File Templates.
 */
@ResultPath("/")
public class GeneralController extends ActionSupport implements ServletResponseAware, ServletRequestAware {

    private CenterCRUDService centroService;
    private List<Centro> centroList = new ArrayList<Centro>();
    private List<String> countries = new ArrayList<String>();
    private List<Integer> centroListSelect = new ArrayList<Integer>();
    private boolean unboundedMonitor;
    private MiembroBms miembroBms = new MiembroBms();
    private String id;
    private Centro site;
    private Collection<CentroAddress> centroAddressesList = new ArrayList<CentroAddress>(0);
    private String investigadorId;
    private InvestigadorProtocoloCentroService investigadorProtocoloCentroService;
    private String investigatorId;
    private String siteId;
    private CdaService cdaService;
    private List<CdaMolecula> cdaMoleculas;
    private InvestigadorProtocoloCentro ipc;
    private ProtocolCRUDService protocolCRUDService;

    public Collection<CentroAddress> getCentroAddressesList() {
        return centroAddressesList;
    }

    public void setCentroAddressesList(Collection<CentroAddress> centroAddressesList) {
        this.centroAddressesList = centroAddressesList;
    }

    @Autowired
    public GeneralController(CenterCRUDService centroService, InvestigadorProtocoloCentroService investigadorProtocoloCentroService, CdaService cdaService, ProtocolCRUDService protocolCRUDService) {
        this.centroService = centroService;
        this.investigadorProtocoloCentroService = investigadorProtocoloCentroService;
        this.cdaService = cdaService;
        this.protocolCRUDService = protocolCRUDService;
    }

    @Action(value="findIPCDataForPIAndSite", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/siteStaffSelection/ipcData.jsp")
    })
    public String findIPCDataForPIAndSite() throws IOException {
        Integer siteId = Integer.parseInt(this.siteId);
        Integer investigatorId = Integer.parseInt(this.investigatorId);
        ipc = investigadorProtocoloCentroService.getIPCDataForPIAndSite(investigatorId, siteId);

//        response.setContentType("text/json");
//        response.getWriter().write("lalala:33488");

        return SUCCESS;
    }

    @Action(value="getSites", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/staffbms/monitorOp.jsp")
    })
    public String getSites() {

         if(countries.size() > 0 && countries.get(0) != null)
            centroList = centroService.getAllByCountries(countries);
         else
             centroList = centroService.list();
        miembroBms.setUnboundedMonitor(unboundedMonitor);

        Centro centro = new Centro(-1,"ALL");
        centroList.add(0,centro);
        return SUCCESS;
    }

    @Action(value="getSiteByInstitutionName", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/investigator/sitesWindow.jsp")
    })
    public String getSiteByInstitutionName() {
        String institution =   site.getInstitucion();
        centroList = centroService.listByInstitution(institution);

        return SUCCESS;
    }

    @Action(value="getSiteAddressesById", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/investigator/sitesMoreInformation.jsp")
    })
    public String getSiteAddressesById() throws NoProtocoloCompromisopaisSelectedException {
        TherapeuticArea protocolTherapeuticArea =
                protocolCRUDService.recoverProtocoloCompromisopais().getProtocolo().getTherapeuticArea();

        Integer centroId = Integer.parseInt(id);
        Centro site = centroService.findById(centroId);
        centroAddressesList = site.getCentroAddresses();
        Iterator<CentroAddress> itAddr = centroAddressesList.iterator();
        Set<Integer> idAddr = new HashSet<Integer>();
        List<CentroAddress> aBorrar = new ArrayList<CentroAddress>();
        while(itAddr.hasNext()){
            CentroAddress actual = itAddr.next();
            if(
                    !idAddr.add(actual.getAddress().getId())
                            ||
                    actual.getTherapeuticArea() == null
                            ||
                    !actual.getTherapeuticArea().equals(protocolTherapeuticArea)){
                aBorrar.add(actual);
            }
        }
        centroAddressesList.removeAll(aBorrar);
        return SUCCESS;
    }

    private Cdamaster cdamaster = new Cdamaster();
    private CdaMolecula cdaMolecula = new CdaMolecula();
    private Integer investigatorid;

    @Action(value="getCdaById", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/investigator/cda.jsp")
    })
    public String getCdaById() {
        Integer centroId = Integer.parseInt(id);
        cdamaster = cdaService.findCdaMasterByCentroId(centroId);
        return SUCCESS;
    }

    @Action(value="findByInvolucradoId", results={
            @Result(name="success",location="WEB-INF/jsp/Protocols/siteStaffSelection/cdaMolecula.jsp")
    })
    public String findByInvolucradoId() {
        Integer id = Integer.parseInt(investigadorId);
        cdaMoleculas = cdaService.findByInvolucradoId(id);

        return SUCCESS;
    }

    public void findByInvolucradoId(Integer id) {

    }

    public List<Centro> getCentroList() {
        return centroList;
    }

    public void setCentroList(List<Centro> centroList) {
        this.centroList = centroList;
    }

    public List<String> getCountries() {
        return countries;
    }

    public void setCountries(List<String> countries) {
        this.countries = countries;
    }

    public List<Integer> getCentroListSelect() {
        return centroListSelect;
    }

    public void setCentroListSelect(List<Integer> centroListSelect) {
        this.centroListSelect = centroListSelect;
    }

    public boolean isUnboundedMonitor() {
        return unboundedMonitor;
    }

    public void setUnboundedMonitor(boolean unboundedMonitor) {
        this.unboundedMonitor = unboundedMonitor;
    }

    public MiembroBms getMiembroBms() {
        return miembroBms;
    }

    public void setMiembroBms(MiembroBms miembroBms) {
        this.miembroBms = miembroBms;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Centro getSite() {
        return site;
    }

    public void setSite(Centro site) {
        this.site = site;
    }

    public Cdamaster getCdamaster() {
        return cdamaster;
    }

    public void setCdamaster(Cdamaster cdamaster) {
        this.cdamaster = cdamaster;
    }

    public CdaMolecula getCdaMolecula() {
        return cdaMolecula;
    }

    public void setCdaMolecula(CdaMolecula cdaMolecula) {
        this.cdaMolecula = cdaMolecula;
    }

    public Integer getInvestigatorid() {
        return investigatorid;
    }

    public void setInvestigatorid(Integer investigatorid) {
        this.investigatorid = investigatorid;
    }

    public List<CdaMolecula> getCdaMoleculas() {
        return cdaMoleculas;
    }

    public void setCdaMoleculas(List<CdaMolecula> cdaMoleculas) {
        this.cdaMoleculas = cdaMoleculas;
    }

    public String getInvestigadorId() {
        return investigadorId;
    }

    public void setInvestigadorId(String investigadorId) {
        this.investigadorId = investigadorId;
    }

    public String getInvestigatorId() {
        return investigatorId;
    }

    public void setInvestigatorId(String investigatorId) {
        this.investigatorId = investigatorId;
    }

    public String getSiteId() {
        return siteId;
    }

    public void setSiteId(String siteId) {
        this.siteId = siteId;
    }

    public InvestigadorProtocoloCentro getIpc() {
        return ipc;
    }

    public void setIpc(InvestigadorProtocoloCentro ipc) {
        this.ipc = ipc;
    }

    private HttpServletRequest request;
    private HttpServletResponse response;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
}
